<?php
namespace App\Repositories;

use App\Models\Club;
use App\Models\ClubTechnician;
use App\Models\Technician;
use App\Models\Topic;
use App\Models\Comment;
use App\Models\CommentDetail;
use App\Models\User;
use App\Models\Forum;
use App\Models\AttachmentRelationship;
use App\Repositories\AttachmentRepository;
use Illuminate\Http\Request;
use App\Events\TopicCreated;

class ClubRepository
{

    public static function isRelationValid($clubId, $technicianId, $beginTime, $endTime = null, $ignore = null)
    {
        $query = ClubTechnician::query()->where('club_id', $clubId)->where('technician_id', $technicianId);
        if ($ignore) {
            $query->where($query->getModel()->getKeyName(), '!=', $ignore);
        }
        if (empty($endTime)) {
            //没有结束时间，看开始时间是否位于其中一个区中间
            $query->where('begin_time', '<', $beginTime)->where('end_time', '>', $beginTime);
        } else {
            //有结束时间，看是否交叉
            $query->where('end_time', '>', $beginTime)->where('begin_time', '<', $endTime);
        }
        $result = $query->get(['id', 'club_name', 'club_short_name', 'technician_name', 'technician_short_name', 'begin_time', 'end_time']);
        if ($result->isNotEmpty()) {
            $result = $result->map(function ($value) {
                return implode(', ', $value->toArray());
            });
            return api(sprintf(
                "开始时间: %s, 结束时间: %s, 有以下记录冲突：\n%s",
                $beginTime, $endTime, $result->implode("\n")
            ));
        } else {
            return api(RET_OK, MSG_OK, []);
        }
    }


}